package net.maku.module.controller;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import net.maku.framework.common.utils.PageResult;
import net.maku.framework.common.utils.Result;
import net.maku.framework.security.user.SecurityUser;
import net.maku.module.entity.AgCarousel;
import net.maku.module.service.AgCarouselService;
import net.maku.module.vo.AgCarouselVO;
import net.maku.system.service.SysUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/carousel")
@Tag(name = "网站轮播图")
@AllArgsConstructor
public class AgCarouselController {

    private final AgCarouselService agCarouselService;
    private final SysUserService sysUserService;
    
    @GetMapping("/page")
    @Operation(summary = "分页查询")
    public Result<PageResult<AgCarouselVO>> page(@RequestParam(defaultValue = "1") Integer pageNum,
                                                     @RequestParam(defaultValue = "10") Integer pageSize,@RequestParam(defaultValue = "")Integer websiteId,@RequestParam(defaultValue = "")Long tenantId) {
        Page<AgCarousel> AgCarouselPage = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<AgCarousel> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ObjectUtil.isNotNull(websiteId), AgCarousel::getWebsiteId, websiteId)
                .eq(ObjectUtil.isNotNull(tenantId), AgCarousel::getTenantId, tenantId);
        IPage<AgCarousel> page = agCarouselService.page(AgCarouselPage,wrapper);
        List<AgCarouselVO> collect = page.getRecords().stream().map(bean -> {
            AgCarouselVO agCarouselVO = new AgCarouselVO();
            BeanUtils.copyProperties(bean, agCarouselVO);
            if (ObjectUtil.isNotNull(bean.getCreator())) {
                agCarouselVO.setCreatedByName(sysUserService.getById(bean.getCreator()).getUsername());
            }
            return agCarouselVO;
        }).collect(Collectors.toList());
        return Result.ok(new PageResult<>(collect, page.getTotal()));
    }

    @GetMapping("/list")
    @Operation(summary = "查询列表")
    public Result<List<AgCarousel>> list(@RequestParam(defaultValue = "")Integer websiteId,@RequestParam(defaultValue = "")Long tenantId) {
        LambdaQueryWrapper<AgCarousel> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ObjectUtil.isNotNull(websiteId), AgCarousel::getWebsiteId, websiteId)
                .eq(ObjectUtil.isNotNull(tenantId), AgCarousel::getTenantId, tenantId);
        List<AgCarousel> list = agCarouselService.list(wrapper);
        return Result.ok(list);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除轮播图")
    public Result<String> delete(@RequestBody List<Long> ids) {
        agCarouselService.removeBatchByIds(ids);
        return Result.ok();
    }

    @PostMapping("/save")
    @Operation(summary = "保存")
    public Result<String> save(@RequestBody AgCarousel agCarousel) {
        agCarousel.setCreateTime(LocalDateTime.now());
        agCarousel.setCreator(SecurityUser.getUserId());
        agCarouselService.save(agCarousel);
        return Result.ok();
    }

}
